package com.itheima.app.log;

import cn.hutool.core.date.DateUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Log;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Aspect
public class LogAspect {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    // 环绕通知   前置、后置
    @Around(value = "@annotation(logWith)") // 哪个方法有此注解，就行aop增强
    public Object aroundLog(ProceedingJoinPoint proceedingJoinPoint, LogWith logWith)throws Throwable {
        System.out.println("aop的日志增强："+logWith.type());
        // 1-1 准备消息
        Log log = new Log();
        log.setUserId(UserHolder.get().getId()); // 操作人id
        log.setLogTime(DateUtil.formatDate(new Date())); // 操作时间
        log.setPlace("三里屯"); // 操作地点
        log.setEquipment("大华为"); //操作设备
        log.setType(logWith.type()); // 操作类型
        // 1-2 发送消息
        rocketMQTemplate.convertAndSend("tanhua-user-log", log);

        // 调用目标对方的原有功能
        return proceedingJoinPoint.proceed();
    }
}
